/**
 * 资源管理
 * @author zzhanglin
 * @returns {ResPool}
 */
ResPool = function()
{
	EventDispatcher.call(this);
	this.className="ResPool";
	this.maxLoadNum = 6 ;//最大的下载数，用于控制并发量
	
	this._resDictionary = [];
	this._loadList =[]; //resVO数组
	this._currentLoadNum = 0 ; //当前在下载第几个
	//用于一次下载多个资源
	this._resArray;//resVO数组
	this._total ;
	this._queueLoaded ;
	this._queueHash ;
};
FLASH.extend (ResPool , EventDispatcher );

//静态变量，私有
ResPool._instance ;
/**
 * 单例模式
 * @returns
 */
ResPool.getInstance = function(){
	if(!ResPool._instance){
		ResPool._instance = new ResPool();
	}
	return ResPool._instance ;
};

/**
 * 通过资源的别名，找资源
 * @param name 别名
 * @returns 资源的ResVO
 */
ResPool.prototype.getResVOByName = function(name)
{
	return this._resDictionary[name];
};
/**
 * 加载资源，用于外部调用 
 * @param resVO
 */
ResPool.prototype.loadRes = function( resVO )
{
	if(!this.checkRes( resVO.name ))
	{
		this._resDictionary[resVO.name] = resVO ;
		if(this._currentLoadNum<this.maxLoadNum)
		{
			this._loadAResource( resVO );//下载资源
		}else{
			this._loadList.push( resVO );
			this._loadList.sort( _compareLoadList );
		}
	}
};
/**
 * 加载一个资源，私有方法
 * @param resVO
 */
ResPool.prototype._loadAResource = function( resVO )
{
	this._resDictionary[resVO.name] = resVO ;
	var loader = new Loader();
	loader.name = resVO.name ;
	loader.addEventListener(Event.COMPLETE , resLoadedHandler);
	loader.load( new URLRequest(resVO.url));
	this._currentLoadNum++;
	var _self = this ;
	//加载完成
	function resLoadedHandler(e) 
	{
		e.stopPropagation();
		
		var resLoader = e.getTarget() ; //Loader
		resLoader.removeEventListener(Event.COMPLETE ,  resLoadedHandler );
		var resVO = _self._resDictionary[resLoader.name] ;
		resVO.content = resLoader.getContent() ;
		//抛出资源加载完成事件
		var resLoadedEvent = new ResLoadedEvent(resVO.name);
		resLoadedEvent.resVO = resVO ;
		_self.dispatchEvent( resLoadedEvent );
		_self._currentLoadNum--;
		//下载下一个
		if( _self._loadList.length>0 )
		{
			resVO = _self._loadList.shift() ;
			_self._loadAResource( resVO );
		}
	}
};
/**
 * 判断是否这个资源已经存在
 * @param resName
 * @returns {Boolean}
 */
ResPool.prototype.checkRes = function( resName )
{
	if(this._resDictionary[resName])
	{
		var resVO = this._resDictionary[resName];
		if(resVO && resVO.content)
		{
			var resLoadedEvent = new ResLoadedEvent(resName);
			resLoadedEvent.resVO = resVO ;
			this.dispatchEvent( resLoadedEvent );
		}
		return true;
	}
	return false;
};
/**
 * 对资源 的优先级下载排序比较
 * @param resVO1
 * @param resVO2
 * @returns {Number}
 */
ResPool.prototype._compareLoadList =function( resVO1 , resVO2 )
{
	return resVO2.priority-resVO1.priority ;
};

//-------------------------加载多个资源------------------------------------------
/**
 * 加载多个 
 * @param resArray :resVO数组
 * @param maxNum最大下载数
 */		
ResPool.prototype.queueLoad = function( resArray , maxNum )
{
	maxNum = maxNum?maxNum:2 ;
	this._queueLoaded =0 ;
	this._resArray = resArray ;
	this._total= this._resArray.length ;
	this._queueHash = [];
	for( var i=0 ; i<maxNum && i<this._total  ; ++i){
		this._startQueueLoad();
	}
};
/**
 * 开始多个资源的序列下载，私有方法
 */
ResPool.prototype._startQueueLoad = function()
{
	var content = this._resArray.shift();
	this._queueHash[content.name] = content;
	//下载资源
	this.addEventListener( content.name,queueResLoadedHandler) ;
	this.loadRes( content );
	//下载完成
	var _self = this ;	
	function queueResLoadedHandler(e) 
	{
		_self._queueLoaded++;
		_self.removeEventListener(e.resVO.name , queueResLoadedHandler  );
		var evt = new ResProgressEvent( ResProgressEvent.RES_LOAD_PROGRESS);
		evt._total = _self._total ;
		evt._loaded = _self._queueLoaded ;
		evt._percent = evt._loaded/evt._total ;
		evt.name = e.resVO.name ;
		_self.dispatchEvent( evt );
		if(_self._resArray.length>0){
			//继续下一个
			_self._startQueueLoad();
		}else if(_self._queueLoaded==_self._total){
			_self.dispatchEvent(new ResLoadedEvent( ResLoadedEvent.RES_QUEUE_LOADED));
		}
	}
};


//==================dispose==================

/**
 * 清除缓存 
 */		
ResPool.prototype.clearPool = function() 
{
	for( var key in this._resDictionary)
	{
		this._resDictionary[key].dispose();
	}
	this._resDictionary = [];
	this._loadList =[]; //resVO数组
	this._currentLoadNum = 0 ; //当前在下载第几个
};